package com.glsc.ngateway.opmanage.controller.desktop;


import com.alibaba.fastjson.JSON;
import com.glsc.ngateway.common.api.common.utils.DateUtil;
import com.glsc.ngateway.common.api.flowable.dto.demand.ResponseUserListSubDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.User;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.ledger.*;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.settlement.SettlementLog;
import com.glsc.ngateway.opmanage.aspect.UserCache;
import com.glsc.ngateway.opmanage.dto.*;
import com.glsc.ngateway.opmanage.service.DesktopService;
import com.glsc.ngateway.opmanage.service.DictionaryService;
import com.glsc.ngateway.opmanage.service.MailService;
import com.glsc.ngateway.opmanage.service.ledger.LedgerAuthService;
import com.glsc.ngateway.opmanage.service.ledger.LedgerService;
import com.glsc.ngateway.opmanage.service.settlement.SettlementLogService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 今日桌面
 */
@RestController
@RequestMapping("/api/desktop")
@Slf4j
public class DesktopController {

    @Resource
    private UserCache userCache;

    @Resource
    private LedgerService ledgerService;

    @Resource
    private DictionaryService dictionaryService;

    @Resource
    private MailService mailService;

    @Resource
    private SettlementLogService settlementLogService;

    @Resource
    private DesktopService desktopService;

    @Resource
    private LedgerAuthService ledgerAuthService;

    /**
     * 今日桌面-根据岗位查询
     *
     * @param request
     * @return
     */
    @GetMapping("/screen")
    public PlatformResponse<?> screen(@ApiParam(value = "id") @RequestParam(required = true) Integer id, HttpServletRequest request) {
        LedgerAuth auth = ledgerAuthService.findById(id);
        if (Objects.isNull(auth)) {
            return PlatformResponse.failedMsg("无数据权限");
        }
        if (auth.getName().contains("股衍运营")) {
            return desktopService.screenYsp(DateUtil.getNowDateString(), request);
        } else if (auth.getName().contains("交易")) {
            // 跳转交易今日桌面
            return desktopService.screenJiaoYi(DateUtil.getNowDateString(), request);
        } else if (auth.getName().contains("固收运营")) {
            // 跳转固收运营今日桌面
            return desktopService.screenGuShou(DateUtil.getNowDateString(), request);
        } else if (auth.getName().contains("综合运营")) {
            // 跳转综合运营今日桌面
            return desktopService.screenZongHe(DateUtil.getNowDateString(), request);
        } else if (auth.getName().contains("清算")) {
            // 跳转清算今日桌面
            return desktopService.screenQingSuan(DateUtil.getNowDateString(), request);
        }


        return PlatformResponse.failedMsg("未查询到数据权限");
    }

    /**
     * 清算记录按钮
     *
     * @param request
     * @return
     */
    @GetMapping("/settlement")
    public PlatformResponse<?> settlement(HttpServletRequest request) {
        User user = userCache.get(request);
        settlementLogService.save(user);
        return PlatformResponse.success();
    }


}